<?php
class BbsService
{
	public function getCreditsTrans()
	{
		global $_FANWE;
		return $_FANWE['setting']['bbs_extcredits'];
	}
	
	public function getECRatio()
	{
		global $_FANWE;
		return $_FANWE['setting']['money_score_ratio'];
	}
	
	public function updateUserMoney($uid,$money)
	{
		$trans = $this->getCreditsTrans();
		DB::query('UPDATE '.DB::table('common_member_count')." SET $trans = $trans $money WHERE uid = '$uid'");
	}
	
	/**
	 * 获取指定分类信息的最新论坛贴子
	 *
	 * @access  public
	 * @param   string  $type  分类信息类型(taste:晒团体验,discount:折扣快讯,shoot:摄影写真,trade:二手团购,complain:团购投诉)
	 * @param   int     $num   贴子数量(默认为5)
	 * @param   int     $gid   关联的团购ID
	 * @return  array
	 */
	public function getNewThreads($type,$num = 5,$gid=0)
	{
		global $_FANWE;
		static $threads = array();
		$key = CURRENT_CITY_PY.'_'.$type.'_'.$num;
		if(empty($threads[$key]))
		{
			switch($type)
			{
				case 'taste':
					$sort_id = TASTE_SORT;
				break;
				
				case 'discount':
					$sort_id = DISCOUNT_SORT;
				break;
				
				case 'shoot':
					$sort_id = SHOOT_SORT;
				break;
				
				case 'trade':
					$sort_id = TRADE_SORT;
					$field = 'trade_type';
					$sql = 'SELECT trade_type FROM '.DB::table('forum_optionvalue'.$sort_id).' WHERE tid=';
				break;
				
				case 'complain':
					$sort_id = COMPLAIN_SORT;
					$field = 'handle_status';
					$sql = 'SELECT handle_status FROM '.DB::table('forum_optionvalue'.$sort_id).' WHERE tid=';
				break;
			}
			
			$join = '';
			if($gid > 0)
			{
				$join.= 'INNER JOIN '.DB::table('forum_typeoptionvar').' AS tovg ON tovg.tid = t.tid 
						AND tovg.optionid = '.GROUP_ID_OPTIONID." AND tovg.value = '$gid' ";
			}
			
			$city_name = $_FANWE['current_city']['name'];
			$query = DB::query("SELECT t.tid,t.subject,t.dateline,(t.attachment = 2) AS is_img,f.name,(tov.value = '$city_name') AS is_current_city 
								FROM ".DB::table('forum_thread').' AS t
								INNER JOIN '.DB::table('forum_forum').' AS f ON f.fid = t.fid 
								INNER JOIN '.DB::table('forum_typeoptionvar').' AS tov ON tov.tid = t.tid 
									AND tov.optionid = '.GROUP_CITY_OPTIONID.' '.$join.'
								WHERE t.closed = 0 AND tov.sortid = '.$sort_id." GROUP BY t.tid 
								ORDER BY is_current_city DESC,t.tid DESC LIMIT 0,$num");
								
			while($data = DB::fetch($query))
			{
				if(isset($field))
					$data[$field] = DB::resultFirst($sql.$data['tid']);
				$data['url'] = U('bbs/thread',array('tid'=>$data['tid']));
				$threads[$key][] = $data;
			}
		}

		return $threads[$key];
	}
	
	/**
	 * 获取最新团购评论回复
	 *
	 * @access  public
	 * @param   int     $num   贴子数量(默认为5)
	 * @return  array
	 */
	public function getNewDiscussionPosts($num = 5)
	{
		global $_FANWE;
		static $posts = array();
		$key = 'num_'.$num;
		if(empty($posts[$key]))
		{
			$city_name = $_FANWE['current_city']['name'];
			//2011-01-30 update
			$query = DB::query("SELECT t.tid,p.pid,p.message,FIND_IN_SET('$city_name',tovc.value) AS is_current_city,tovg.value AS gid 
								FROM ".DB::table('forum_thread').' AS t
								INNER JOIN '.DB::table('forum_typeoptionvar').' AS tovc ON tovc.tid = t.tid 
									AND tovc.optionid = '.GROUP_CITY_OPTIONID.' AND tovc.value <> \'\' 
								INNER JOIN '.DB::table('forum_typeoptionvar').' AS tovg ON tovg.tid = t.tid 
									AND tovg.optionid = '.GROUP_ID_OPTIONID.' AND tovg.value <> \'\' 
								INNER JOIN '.DB::table('forum_post').' AS p ON 
									p.tid = t.tid AND p.first = 0 AND p.dateline = t.lastpost 
								WHERE t.closed = 0 AND t.sortid = '.DISCUSSION_SORT.' 
								GROUP BY t.tid ORDER BY is_current_city DESC,t.lastpost DESC LIMIT 0,'.$num);
								
			while($data = DB::fetch($query))
			{
				$data['message'] = preg_replace("/[\r\n]/is",'',$data['message']);
				
				$message = trim(preg_replace("/\[(.+)(?:|=.+)\].*\[\/\\1\]/is",'',$data['message']));
				
				if(!empty($message))
					$data['message'] = $message;
				else
				{
					$message = trim(preg_replace("/\[(.+?)\]/is",'',$data['message']));
					if(!empty($message))
						$data['message'] = $message;
				}
					
				$data['small_img'] = DB::resultFirst('SELECT small_img FROM '.DB::table('goods').' WHERE id = '.$data['gid']);
				if(!parseUrl($data['small_img']))
	 				$data['small_img'] = SITE_PATH.$data['small_img'];
				
				$data['goods_url'] = U("goods/show",array("id"=>$data['gid']));
				$data['url'] = U('bbs/redirect',array('goto'=>'findpost','ptid'=>$data['tid'],'pid'=>$data['pid']));
				$posts[$key][] = $data;
			}
		}
		
		return $posts[$key];
	}
	
	public function getPostCountByGoods($gid)
	{
		return intval(DB::resultFirst('SELECT SUM(t.replies) FROM '.DB::table('forum_typeoptionvar').' AS tov 
			INNER JOIN '.DB::table('forum_thread').' AS t ON t.tid = tov.tid 
			WHERE tov.optionid = '.GROUP_ID_OPTIONID." AND tov.value = '$gid' AND t.sortid in (".DISCUSSION_SORT.") AND t.closed>=0"));
	}
	
	public function getSortType($sortid)
	{
		static $types = array();
		
		if(empty($types[$sortid]))
		{
			$ids = explode(',',NORMAL_GROUP_TRADE);
			if(in_array($sortid,$ids))
			{
				$types[$sortid] = 'NORMAL_GROUP';
				return 'NORMAL_GROUP';
			}
			
			$ids = explode(',',CLASS_GROUP_TRADE);
			if(in_array($sortid,$ids))
			{
				$types[$sortid] = 'CLASS_GROUP';
				return 'CLASS_GROUP';
			}
			
			$ids = explode(',',GROUP_COMPLAIN_TRADE);
			if(in_array($sortid,$ids))
			{
				$types[$sortid] = 'GROUP_COMPLAIN';
				return 'GROUP_COMPLAIN';
			}
			
			$ids = explode(',',TRADE_GROUP_SORTID);
			if(in_array($sortid,$ids))
			{
				$types[$sortid] = 'TRADE_GROUP';
				return 'TRADE_GROUP';
			}
		}
		
		return $types[$sortid];
	}
}
?>